import { Body, Controller, Post, Req } from '@nestjs/common';
import { OrderService } from "./order.service";
import { ApiBasicAuth, ApiBody, ApiExtraModels, ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
import { QueryOrderVo } from 'src/vo/order/query-order.vo';
import { CreateOrderVo } from 'src/vo/order/create-order.vo';
import { EntityManager, Transaction, TransactionManager } from 'typeorm';
import { CancelOrderVo } from 'src/vo/order/cancel-order.vo';
import { ApiResult } from 'src/decorators/api-result.decorator';
import { OrderListResVo } from 'src/vo/order/order-list-res.vo';
import { ResultVo } from 'src/vo/common/result.vo';
import { CreateOrderResVo } from 'src/vo/order/create-order-res.vo';
import {DeliveryAddressEntity} from "../deliveryAddress/delivery-address.entity";
import {OrderDetailVo} from "../../vo/order/order-detail.vo";
import {IntegralPayVo} from "../../vo/order/integral-pay.vo";

@ApiExtraModels(OrderListResVo, CreateOrderResVo, ResultVo)
@Controller('order')
@ApiTags('订单管理')
@ApiBasicAuth("token")
export class OrderController {
    constructor(private readonly orderService: OrderService) { }

    @Post("queryOrdersByPage")
    @ApiOperation({ summary: '分页获取订单列表接口' })
    @ApiResult(OrderListResVo, true, true)
    async queryProductsByPage(@Body() queryOrderVo: QueryOrderVo) {
        return await this.orderService.queryOrdersByPage(queryOrderVo);
    }

    @Post("queryByOrderCode")
    @ApiResult(DeliveryAddressEntity)
    @ApiOperation({ summary: '根据orderCode查询详情' })
    async queryByOrderCode(@Body() data: OrderDetailVo) {
        return await this.orderService.queryByOrderCode(data.userId,data.orderCode);
    }

    @Post("createOrder")
    @ApiOperation({ summary: '创建订单' })
    @ApiResult(CreateOrderResVo)
    @ApiBody({ type: CreateOrderVo, description: '创建订单vo' })
    @Transaction()
    async createOrder(@Body() createOrderVo: CreateOrderVo, @TransactionManager() manager: EntityManager) {
        return await this.orderService.createOrder(createOrderVo, manager);
    }

    @Post("cancelOrder")
    @ApiOperation({ summary: '取消订单' })
    @ApiResult()
    @ApiBody({ type: CancelOrderVo, description: '取消订单vo' })
    @Transaction()
    async cancelOrder(@Body() cancelOrderVo: CancelOrderVo, @TransactionManager() manager: EntityManager) {
        return await this.orderService.cancelOrder(cancelOrderVo, manager);
    }

    @Post("integralPay")
    @ApiOperation({ summary: '积分支付' })
    @ApiResult()
    @ApiBody({ type: IntegralPayVo, description: '积分支付' })
    @Transaction()
    async integralPay(@Body() integralPayVo: IntegralPayVo, @TransactionManager() manager: EntityManager) {
        return await this.orderService.integralPay(integralPayVo, manager);
    }

    @Post("againBuy")
    @ApiOperation({ summary: '再次购买' })
    @ApiResult()
    @ApiBody({ type: OrderDetailVo, description: '再次购买' })
    @Transaction()
    async againBuy(@Body() orderDetailVo: OrderDetailVo, @TransactionManager() manager: EntityManager) {
        return await this.orderService.againBuy(orderDetailVo, manager);
    }

}
